﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public GameObject[] ShapeGameObjects;
    public Text RoundText;
    public Player[] Players;
    public GameObject Dialog;
    public Text DialogText;
    public GameObject PlayerOneBG;
    public GameObject PlayerTwoBG;
    public static int CurrentPlayer { get; private set; }
    private static GameManager instance;
    public static GameManager Instance { get { return instance; } }
    void Awake()
    {
        instance = this;
        Players[0].ShapeGameObjects = ShapeGameObjects;
        Players[1].ShapeGameObjects = ShapeGameObjects;
    }
	// Use this for initialization
	void Start ()
	{
	    CurrentPlayer = 1;
        PlayerOneBG.SetActive(true);
        PlayerTwoBG.SetActive(false);

        SetRound();
	}

    public void ChangeRound()
    {
        if (Cards.IsCanFallDown == false)
        {
            Dialog.SetActive(true);
            DialogText.text = "请正确放置方块后结束回合";
            return;
        }

        MapItem.PlaceAction();
        Player.FallDownAction();

        switch (CurrentPlayer)
        {
            case 1:
                CurrentPlayer = 2;
                PlayerOneBG.SetActive(false);
                PlayerTwoBG.SetActive(true);
                break;
            case 2:
                CurrentPlayer = 1;
                PlayerOneBG.SetActive(true);
                PlayerTwoBG.SetActive(false);
                break;
        }
        
        Cards.IsCanFallDown = false;
        Cards.CurrntCardInHandGameObject = null;
        GameState.CurrentModeIndex = 0;
        MapItem.ResetSelectedStateAction();
        SetRound();
    }

    public void RotateMode()
    {
        if(GameState.CurrentModeKinds == 0)
            return;
        GameState.CurrentModeIndex = (GameState.CurrentModeIndex + 1) % GameState.CurrentModeKinds;
        Player.RotateShowAction();
    }

    public void Cancel()
    {
        Cards.CurrntCardInHandGameObject.SetActive(true);
        Cards.CurrntCardInHandGameObject = null;
        MapItem.ResetSelectedStateAction();
        GameState.CurrentModeIndex = 0;
    }

    private void SetPlayingState(int player)
    {
        if (player == 0)
        {
            CurrentPlayer = 1;
            Players[0].IsPlaying = true;
            Players[1].IsPlaying = false;
        }
        else
        {
            CurrentPlayer = 2;
            Players[0].IsPlaying = false;
            Players[1].IsPlaying = true;
        }
    }
    private void SetRound()
    {
        RoundText.gameObject.SetActive(true);
        switch (CurrentPlayer)
        {
            case 1:
                RoundText.text = "PlayerOne Round";
                SetPlayingState(0);
                Players[0].StartRoundAction();
                break;
            case 2:
                RoundText.text = "PlayerTwo Round";
                SetPlayingState(1);
                Players[1].StartRoundAction();
                break;
        }
        StartCoroutine(Wait());
    }

    IEnumerator Wait()
    {
        yield return new WaitForSeconds(1);
        RoundText.gameObject.SetActive(false);
    }
}

public enum Shape
{
    Fang,
    UI_LLeft,
    UI_LRight,
    UI_T,
    UI_Tiao,
    UI_ZLeft,
    UI_ZRight
}
